package cn.com.dhc.danlu.activity.delivery.order.adatper;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.com.dhc.danlu.activity.delivery.DeliveryDetailActivity;
import cn.com.dhc.danlu.activity.delivery.order.bean.OrderStatusBean;
import cn.com.dhc.danlu.activity.delivery.order.decorate.OrderVisitable;
import cn.com.dhc.danlu.activity.delivery.order.decorate.Visitable;
import cn.com.dhc.danlu.activity.delivery.order.delegate.EventType;
import cn.com.dhc.danlu.activity.delivery.order.delegate.IHolderDataCallback;
import cn.com.dhc.danlu.activity.delivery.order.typefactory.IOrderTypeFactory;
import cn.com.dhc.danlu.activity.delivery.order.typefactory.OrderTypeFactory;
import cn.com.dhc.danlu.activity.delivery.order.viewholder.BaseViewHolder;
import cn.com.dhc.danlu.widget.adapter.BaseRecyclerviewAdapter;

import static cn.com.dhc.danlu.util.CommonConst.MyOrderConst.DEAL_COMPLETE;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * author: 吴海洋(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-24 04:49<br/>
 *
 * <p>
 * 我的订单 多 type adapter
 * </p>
 */
public class MultiOrderRecyclerAdapter extends BaseRecyclerviewAdapter<BaseViewHolder> implements IHolderDataCallback {
    private IOrderTypeFactory orderTypeFactory;

    public MultiOrderRecyclerAdapter() {
        super();
        this.orderTypeFactory = new OrderTypeFactory();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        return orderTypeFactory.createViewHolder(viewType, view, this);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        final OrderVisitable visitable = (OrderVisitable) getItemByPos(position);
        holder.setData(visitable);
        // 点击跳转到商品详情页
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DeliveryDetailActivity.startSelf(v.getContext(), visitable.getImpOrderNo());
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return ((Visitable) getItemByPos(position)).type(orderTypeFactory);
    }

    @Override
    public void onDataChanged(EventType type, int originalPos, int goodsCount, String newButtomList) {
        int headerPos = originalPos - goodsCount;
        OrderStatusBean bean = (OrderStatusBean) getItemByPos(headerPos - 2);
        if (type == EventType.cancelDeal) {
            bean.setButtomList(newButtomList);
        } else if (type == EventType.receiveGoods) {
            // 将交易状态改为已完成
            bean.setOrderStatus(DEAL_COMPLETE);
            bean.setButtomList(newButtomList);
        }
        notifyDataSetChanged();
    }
}
